package com.hyjx.framework.service.session;

import com.hyjx.framework.service.SystemService;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Session接口服务的 本地实现
 *
 * @author peter
 */
@Component("baseSession")
public class SessionLocal implements ISession {
    private HttpSession getSession() {
        return SystemService.getRequest().getSession(true);
    }
    
    /**
     * 设置Session对象
     *
     * @param key   Session的键
     * @param value Session的值
     */
    public void set(String key, Object value) {
        getSession().setAttribute(key, value);
    }
    
    public void setByRequest(HttpServletRequest request, String key, Object value) {
        request.getSession().setAttribute(key, value);
    }
    
    /**
     * 获取Session对象
     *
     * @param key Session的键
     */
    public Object get(String key) {
        return getSession().getAttribute(key);
    }
    
    /**
     * 移除Session对象
     *
     * @param key Session的键
     */
    public void remove(String key) {
        getSession().removeAttribute(key);
    }
    
    @Override
    public String getString(String key) {
        Object obj = get(key);
        
        if (obj != null) {
            return (String) obj;
        } else {
            return null;
        }
    }
    
    @Override
    public void invalidate() {
        getSession().invalidate();
    }
    
    @Override
    public void forceSet(String key, Object value) {
        set(key, value);
    }
}